<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 5.4.0">


  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">

<link rel="stylesheet" href="/css/main.css">



<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free@5.15.2/css/all.min.css">
  <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/animate.css@3.1.1/animate.min.css">

<script class="hexo-configurations">
    var NexT = window.NexT || {};
    var CONFIG = {"hostname":"littlefxc.github.io","root":"/","images":"/images","scheme":"Mist","version":"8.2.2","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12},"copycode":false,"bookmark":{"enable":false,"color":"#222","save":"auto"},"fancybox":false,"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"fadeInDown","post_body":"fadeInDown","coll_header":"fadeInLeft","sidebar":"fadeInUp"}},"prism":false,"i18n":{"placeholder":"搜索...","empty":"没有找到任何搜索结果：${query}","hits_time":"找到 ${hits} 个搜索结果（用时 ${time} 毫秒）","hits":"找到 ${hits} 个搜索结果"},"path":"/search.xml","localsearch":{"enable":true,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false}};
  </script>
<meta property="og:type" content="website">
<meta property="og:title" content="一年春又来">
<meta property="og:url" content="http://littlefxc.github.io/page/5/index.html">
<meta property="og:site_name" content="一年春又来">
<meta property="og:locale" content="zh_CN">
<meta property="article:author" content="一年春又来">
<meta name="twitter:card" content="summary">


<link rel="canonical" href="http://littlefxc.github.io/page/5/">


<script class="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome : true,
    isPost : false,
    lang   : 'zh-CN'
  };
</script>
<title>一年春又来</title>
  




  <noscript>
  <style>
  body { margin-top: 2rem; }

  .use-motion .menu-item,
  .use-motion .sidebar,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header {
    visibility: visible;
  }

  .use-motion .header,
  .use-motion .site-brand-container .toggle,
  .use-motion .footer { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle,
  .use-motion .custom-logo-image {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line {
    transform: scaleX(1);
  }

  .search-pop-overlay, .sidebar-nav { display: none; }
  .sidebar-panel { display: block; }
  </style>
</noscript>

<link rel="alternate" href="/atom.xml" title="一年春又来" type="application/atom+xml">
</head>

<body itemscope itemtype="http://schema.org/WebPage" class="use-motion">
  <div class="headband"></div>

  <main class="main">
    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏" role="button">
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <i class="logo-line"></i>
      <h1 class="site-title">一年春又来</h1>
      <i class="logo-line"></i>
    </a>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
        <i class="fa fa-search fa-fw fa-lg"></i>
    </div>
  </div>
</div>



<nav class="site-nav">
  <ul class="main-menu menu">
        <li class="menu-item menu-item-home"><a href="/" rel="section"><i class="home                          //首页 fa-fw"></i>首页</a></li>
        <li class="menu-item menu-item-archives"><a href="/archives/" rel="section"><i class="archive          //归档 fa-fw"></i>归档</a></li>
        <li class="menu-item menu-item-categories"><a href="/categories/" rel="section"><i class="th           //分类 fa-fw"></i>分类</a></li>
        <li class="menu-item menu-item-tags"><a href="/tags/" rel="section"><i class="tags                     //标签 fa-fw"></i>标签</a></li>
      <li class="menu-item menu-item-search">
        <a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>搜索
        </a>
      </li>
  </ul>
</nav>



  <div class="search-pop-overlay">
    <div class="popup search-popup"><div class="search-header">
  <span class="search-icon">
    <i class="fa fa-search"></i>
  </span>
  <div class="search-input-container">
    <input autocomplete="off" autocapitalize="off" maxlength="80"
           placeholder="搜索..." spellcheck="false"
           type="search" class="search-input">
  </div>
  <span class="popup-btn-close" role="button">
    <i class="fa fa-times-circle"></i>
  </span>
</div>
<div class="search-result-container no-result">
  <div class="search-result-icon">
    <i class="fa fa-spinner fa-pulse fa-5x"></i>
  </div>
</div>

    </div>
  </div>

</div>
        
  
  <div class="toggle sidebar-toggle" role="button">
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
  </div>

  <aside class="sidebar">

    <div class="sidebar-inner sidebar-overview-active">
      <ul class="sidebar-nav">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <div class="sidebar-panel-container">
        <!--noindex-->
        <div class="post-toc-wrap sidebar-panel">
        </div>
        <!--/noindex-->

        <div class="site-overview-wrap sidebar-panel">
          <div class="site-author site-overview-item animated" itemprop="author" itemscope itemtype="http://schema.org/Person">
  <p class="site-author-name" itemprop="name">一年春又来</p>
  <div class="site-description" itemprop="description"></div>
</div>
<div class="site-state-wrap site-overview-item animated">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/archives/">
        
          <span class="site-state-item-count">234</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
            <a href="/categories/">
          
        <span class="site-state-item-count">38</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
            <a href="/tags/">
          
        <span class="site-state-item-count">125</span>
        <span class="site-state-item-name">标签</span></a>
      </div>
  </nav>
</div>



        </div>
      </div>
    </div>
  </aside>
  <div class="sidebar-dimmer"></div>


    </header>

    
  <div class="back-to-top" role="button">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>

<noscript>
  <div class="noscript-warning">Theme NexT works best with JavaScript enabled</div>
</noscript>


    <div class="main-inner index posts-expand">

    


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="">
    <link itemprop="mainEntityOfPage" href="http://littlefxc.github.io/2021/08/04/%E7%BA%BF%E7%A8%8B%E6%B1%A0ThreadPoolExecutor/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.gif">
      <meta itemprop="name" content="一年春又来">
      <meta itemprop="description" content="">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="一年春又来">
    </span>
      <header class="post-header">
        <h2 class="post-title" itemprop="name headline">
          <a href="/2021/08/04/%E7%BA%BF%E7%A8%8B%E6%B1%A0ThreadPoolExecutor/" class="post-title-link" itemprop="url">线程池ThreadPoolExecutor</a>
        </h2>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">发表于</span>

      <time title="创建时间：2021-08-04 22:01:38" itemprop="dateCreated datePublished" datetime="2021-08-04T22:01:38+08:00">2021-08-04</time>
    </span>
      <span class="post-meta-item">
        <span class="post-meta-item-icon">
          <i class="far fa-calendar-check"></i>
        </span>
        <span class="post-meta-item-text">更新于</span>
        <time title="修改时间：2021-08-05 23:30:59" itemprop="dateModified" datetime="2021-08-05T23:30:59+08:00">2021-08-05</time>
      </span>

  
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">
          <h1 id="前言"><a href="#前言" class="headerlink" title="前言"></a>前言</h1><p>线程就是一个放线程的池子。</p>
<p>使用线程池的好处：</p>
<ul>
<li>重用已存在的线程，从而减少对象创建和销毁的开销。</li>
<li>控制并发，从而提高资源利用率，有效避免过多的资源竞争，提升性能</li>
<li>功能强大，有定时执行、定期执行、单线程执行、并发控制等等</li>
</ul>
          <!--noindex-->
            <div class="post-button">
              <a class="btn" href="/2021/08/04/%E7%BA%BF%E7%A8%8B%E6%B1%A0ThreadPoolExecutor/#more" rel="contents">
                阅读全文 &raquo;
              </a>
            </div>
          <!--/noindex-->
        
      
    </div>

    
    
    

    <footer class="post-footer">
        <div class="post-eof"></div>
      
    </footer>
  </article>
</div>




    


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="">
    <link itemprop="mainEntityOfPage" href="http://littlefxc.github.io/2021/08/02/ab%E4%BD%BF%E7%94%A8%E6%95%99%E7%A8%8B/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.gif">
      <meta itemprop="name" content="一年春又来">
      <meta itemprop="description" content="">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="一年春又来">
    </span>
      <header class="post-header">
        <h2 class="post-title" itemprop="name headline">
          <a href="/2021/08/02/ab%E4%BD%BF%E7%94%A8%E6%95%99%E7%A8%8B/" class="post-title-link" itemprop="url">ab使用教程</a>
        </h2>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">发表于</span>
      

      <time title="创建时间：2021-08-02 09:26:12 / 修改时间：09:32:52" itemprop="dateCreated datePublished" datetime="2021-08-02T09:26:12+08:00">2021-08-02</time>
    </span>

  
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">
          <h1 id="前言"><a href="#前言" class="headerlink" title="前言"></a><strong>前言</strong></h1><p>ab全称Apache HTTP server benchmarking tool的缩写，是Apache自带的网站benchmark测试工具。使用非常方便，简约而不简单。</p>
<p>官方网站：<a target="_blank" rel="noopener" href="https://httpd.apache.org/docs/2.4/programs/ab.html">https://httpd.apache.org/docs/2.4/programs/ab.html</a></p>
<blockquote>
<p><strong>TIPS</strong></p>
<p>可达到ab相同效果的工具： </p>
<ul>
<li><p>wrk：<a target="_blank" rel="noopener" href="https://github.com/wg/wrk">https://github.com/wg/wrk</a> </p>
</li>
<li><p>wrk2：<a target="_blank" rel="noopener" href="https://github.com/giltene/wrk2">https://github.com/giltene/wrk2</a> </p>
</li>
<li><p>JMeter：<a target="_blank" rel="noopener" href="https://jmeter.apache.org/">https://jmeter.apache.org/</a> </p>
</li>
<li><p>Locust：<a target="_blank" rel="noopener" href="https://locust.io/">https://locust.io/</a></p>
</li>
</ul>
</blockquote>
<h2 id="安装"><a href="#安装" class="headerlink" title="安装"></a><strong>安装</strong></h2><h3 id="macOS"><a href="#macOS" class="headerlink" title="macOS"></a><strong>macOS</strong></h3><p>系统自带，直接使用即可。</p>
<h3 id="CentOS-7"><a href="#CentOS-7" class="headerlink" title="CentOS 7"></a><strong>CentOS 7</strong></h3><p>运行如下命令即可安装</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">yum -y install httpd-tools</span><br></pre></td></tr></table></figure>

<h3 id="Windows"><a href="#Windows" class="headerlink" title="Windows"></a><strong>Windows</strong></h3><ul>
<li>前往 <a target="_blank" rel="noopener" href="https://www.apachehaus.com/cgi-bin/download.plx">https://www.apachehaus.com/cgi-bin/download.plx</a> 下载ab安装包。</li>
<li>解压，将目录切换到安装包的bin目录，并在终端中执行如下命令即可</li>
</ul>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">./ab xxxx</span><br></pre></td></tr></table></figure>

<blockquote>
<p><strong>TIPS</strong></p>
<p>由于笔者没有Windows环境，所以此步骤无法亲测。<br>亲们可百度 “Windows 安装 ab”，参照结果安装即可。资料非常丰富。</p>
</blockquote>
<h2 id="使用说明"><a href="#使用说明" class="headerlink" title="使用说明"></a><strong>使用说明</strong></h2><p>ab是一个命令行工具，使用说明如下：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br></pre></td><td class="code"><pre><span class="line">➜ ab -help</span><br><span class="line"></span><br><span class="line">Usage: ab [options] [http[s]://]hostname[:port]/path</span><br><span class="line"></span><br><span class="line">Options are:</span><br><span class="line"></span><br><span class="line">  -n requests   指定请求多少次</span><br><span class="line"></span><br><span class="line">  -c concurrency 指定并发数(指定同时发送几个请求)</span><br><span class="line"></span><br><span class="line">  -t timelimit  测试持续的最长时间，默认无限制，此参数隐含-n 50000</span><br><span class="line"></span><br><span class="line">  -s timeout   每个请求的超时时间，默认30秒</span><br><span class="line"></span><br><span class="line">  -b windowsize  指定TCP收发缓存大小，单位字节</span><br><span class="line"></span><br><span class="line">  -B address   指定在发起连接时绑定的IP地址</span><br><span class="line"></span><br><span class="line">  -p postfile   指定想要POST的文件，需和-T参数配合使用</span><br><span class="line"></span><br><span class="line">  -u putfile   指定想要PUT的文件，需和-T参数配合使用</span><br><span class="line"></span><br><span class="line">  -T content-type 指定POST/PUT文件时的Content-type。默认text/plain</span><br><span class="line"></span><br><span class="line">  -v verbosity  详细模式，打印更多日志</span><br><span class="line"></span><br><span class="line">  -w       将结果输出到html表格中</span><br><span class="line"></span><br><span class="line">  -i       使用HEAD方式代替GET发起请求</span><br><span class="line"></span><br><span class="line">  -x attributes  插入字符串作为table标签的属性</span><br><span class="line"></span><br><span class="line">  -y attributes  插入字符串作为tr标签的属性</span><br><span class="line"></span><br><span class="line">  -z attributes  插入字符串作为td或th标签的属性</span><br><span class="line"></span><br><span class="line">  -C attribute  添加Cookie，例如Apache=1234；可重复该参数选项以添加多个Cookie</span><br><span class="line"></span><br><span class="line">  -H attribute  添加任意的请求头，例如Accept-Encoding: gzip；可重复该参数选项以添加多个</span><br><span class="line"></span><br><span class="line">  -A attribute  添加Basic WWW认证信息，用户名和密码之间用:分隔</span><br><span class="line"></span><br><span class="line">  -P attribute  添加Basic Proxy认证信息，用户名和密码之间用:分隔</span><br><span class="line"></span><br><span class="line">  -X proxy:port  指定代理地址</span><br><span class="line"></span><br><span class="line">  -V       打印ab的版本信息</span><br><span class="line"></span><br><span class="line">  -k       使用HTTP的KeepAlive特性</span><br><span class="line"></span><br><span class="line">  -d       不显示百分比</span><br><span class="line"></span><br><span class="line">  -S       不显示预估和警告信息</span><br><span class="line"></span><br><span class="line">  -q       默认情况下，如果处理的请求数大于150，ab每处理大约10%或者100个请求时，打印一下进度信息。使用该参数后则不打印进度。</span><br><span class="line"></span><br><span class="line">  -g filename   输出结果信息到gnuplot格式的文件中</span><br><span class="line"></span><br><span class="line">  -e filename   输出结果信息到CSV格式的文件中</span><br><span class="line"></span><br><span class="line">  -r       指定接收到错误信息时不退出程序</span><br><span class="line"></span><br><span class="line">  -h       显示使用说明</span><br><span class="line"></span><br><span class="line">  -Z ciphersuite 指定SSL/TLS密码套件</span><br><span class="line"></span><br><span class="line">  -f protocol   指定SSL/TLS协议(SSL3, TLS1, TLS1.1, TLS1.2 or ALL)</span><br></pre></td></tr></table></figure>

<h2 id="示例"><a href="#示例" class="headerlink" title="示例"></a><strong>示例</strong></h2><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">ab -c 100 -n 10000 http://www.baidu.com/</span><br></pre></td></tr></table></figure>

<p>表示并发100，请求baidu 10000次。</p>
<blockquote>
<p><strong>TIPS</strong></p>
<p><a target="_blank" rel="noopener" href="http://www.baidu.com/">http://www.baidu.com/</a> 不能写成 <a target="_blank" rel="noopener" href="http://www.baidu.com/">http://www.baidu.com</a> 。</p>
<p>这是因为 <a target="_blank" rel="noopener" href="http://www.baidu.com/">http://www.baidu.com</a> 不符合ab要求的 [http[s]://]hostname[:port]/path 格式要求。这是个小坑，需要注意一下。</p>
</blockquote>
<h2 id="报表解读"><a href="#报表解读" class="headerlink" title="报表解读"></a><strong>报表解读</strong></h2><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br></pre></td><td class="code"><pre><span class="line">➜ ab -c 10 -n 1000 http://www.baidu.com/</span><br><span class="line"></span><br><span class="line">This is ApacheBench, Version 2.3 &lt;$Revision: 1843412 $&gt;</span><br><span class="line"></span><br><span class="line">Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/</span><br><span class="line"></span><br><span class="line">Licensed to The Apache Software Foundation, http://www.apache.org/</span><br><span class="line"></span><br><span class="line">Benchmarking www.baidu.com (be patient)</span><br><span class="line"></span><br><span class="line">Completed 100 requests</span><br><span class="line"></span><br><span class="line">Completed 200 requests</span><br><span class="line"></span><br><span class="line">Completed 300 requests</span><br><span class="line"></span><br><span class="line">Completed 400 requests</span><br><span class="line"></span><br><span class="line">Completed 500 requests</span><br><span class="line"></span><br><span class="line">Completed 600 requests</span><br><span class="line"></span><br><span class="line">Completed 700 requests</span><br><span class="line"></span><br><span class="line">Completed 800 requests</span><br><span class="line"></span><br><span class="line">Completed 900 requests</span><br><span class="line"></span><br><span class="line">Completed 1000 requests</span><br><span class="line"></span><br><span class="line">Finished 1000 requests</span><br><span class="line"></span><br><span class="line"><span class="meta">#</span><span class="bash"> 展示测试地址所使用的服务器软件及版本</span></span><br><span class="line">Server Software:    BWS/1.1</span><br><span class="line"><span class="meta">#</span><span class="bash"> 测试地址的主机名</span></span><br><span class="line">Server Hostname:    www.baidu.com</span><br><span class="line"><span class="meta">#</span><span class="bash"> 测试地址的端口</span></span><br><span class="line">Server Port:      80</span><br><span class="line"><span class="meta">#</span><span class="bash"> 测试地址的路径</span></span><br><span class="line">Document Path:     /</span><br><span class="line"><span class="meta">#</span><span class="bash"> 测试地址的文档大小</span></span><br><span class="line">Document Length:    255598 bytes</span><br><span class="line"><span class="meta">#</span><span class="bash"> 并发数</span></span><br><span class="line">Concurrency Level:   10</span><br><span class="line"><span class="meta">#</span><span class="bash"> 测试花费了多久</span></span><br><span class="line">Time taken for tests:  81.881 seconds</span><br><span class="line"><span class="meta">#</span><span class="bash"> 测试总共请求了多少次</span></span><br><span class="line">Complete requests:   1000</span><br><span class="line"><span class="meta">#</span><span class="bash"> 失败的请求数</span></span><br><span class="line">Failed requests:    979</span><br><span class="line"></span><br><span class="line">  (Connect: 0, Receive: 0, Length: 979, Exceptions: 0)</span><br><span class="line"></span><br><span class="line"><span class="meta">#</span><span class="bash"> 传输的总数据量</span></span><br><span class="line">Total transferred:   256761473 bytes</span><br><span class="line"></span><br><span class="line"><span class="meta">#</span><span class="bash"> HTML文档的总数据量</span></span><br><span class="line">HTML transferred:    255605512 bytes</span><br><span class="line"></span><br><span class="line"><span class="meta">#</span><span class="bash"> 平均每秒的请求数，也叫RPS，该值越大表示服务器吞吐量越大，性能表现越好</span></span><br><span class="line">Requests per second:  12.21 [#/sec] (mean)</span><br><span class="line"></span><br><span class="line"><span class="meta">#</span><span class="bash"> 请求平均耗时，越小说明响应越快</span></span><br><span class="line">Time per request:    818.815 [ms] (mean)</span><br><span class="line"></span><br><span class="line"><span class="meta">#</span><span class="bash"> 服务器平均处理时间，其实是服务器吞吐量的倒数</span></span><br><span class="line">Time per request:    81.881 [ms] (mean, across all concurrent requests)</span><br><span class="line"></span><br><span class="line"><span class="meta">#</span><span class="bash"> 每秒获取的数据长度，单位单位：KB/s</span></span><br><span class="line">Transfer rate:     3062.28 [Kbytes/sec] received</span><br><span class="line"></span><br><span class="line"><span class="meta">#</span><span class="bash"> 连接时间统计信息</span></span><br><span class="line">Connection Times (ms)</span><br><span class="line"></span><br><span class="line"><span class="meta">#</span><span class="bash">       最小 平均     中值  最大</span></span><br><span class="line"></span><br><span class="line">​       min mean[+/-sd] median  max</span><br><span class="line"></span><br><span class="line"><span class="meta">#</span><span class="bash"> 连接时间</span></span><br><span class="line">Connect:    17 154 118.0  135  1416</span><br><span class="line"><span class="meta">#</span><span class="bash"> 处理时间</span></span><br><span class="line">Processing:  329 661 211.2  602  1824</span><br><span class="line"><span class="meta">#</span><span class="bash"> 等待时间</span></span><br><span class="line">Waiting:    22 160 88.4  143  1089</span><br><span class="line"><span class="meta">#</span><span class="bash"> 总计时间</span></span><br><span class="line">Total:    391 816 263.4  731  2521</span><br><span class="line"></span><br><span class="line"><span class="meta">#</span><span class="bash"> 请求耗时的统计信息。例如请求经过排序后，50百分位的请求花费了731毫秒；99百分位的请求花费了1942毫秒等</span></span><br><span class="line">Percentage of the requests served within a certain time (ms)</span><br><span class="line"></span><br><span class="line"><span class="meta"> 50%</span><span class="bash">  731</span></span><br><span class="line"></span><br><span class="line"><span class="meta"> 66%</span><span class="bash">  804</span></span><br><span class="line"></span><br><span class="line"><span class="meta"> 75%</span><span class="bash">  873</span></span><br><span class="line"></span><br><span class="line"><span class="meta"> 80%</span><span class="bash">  943</span></span><br><span class="line"></span><br><span class="line"><span class="meta"> 90%</span><span class="bash">  1154</span></span><br><span class="line"></span><br><span class="line"><span class="meta"> 95%</span><span class="bash">  1332</span></span><br><span class="line"></span><br><span class="line"><span class="meta"> 98%</span><span class="bash">  1726</span></span><br><span class="line"></span><br><span class="line"><span class="meta"> 99%</span><span class="bash">  1942</span></span><br><span class="line"></span><br><span class="line"><span class="meta">100%</span><span class="bash">  2521 (longest request)</span></span><br></pre></td></tr></table></figure>
      
    </div>

    
    
    

    <footer class="post-footer">
        <div class="post-eof"></div>
      
    </footer>
  </article>
</div>




    


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="">
    <link itemprop="mainEntityOfPage" href="http://littlefxc.github.io/2021/08/01/docs/pool/%E5%AF%B9%E8%B1%A1%E6%B1%A0%E5%AE%9E%E6%88%98-%E7%BC%96%E5%86%99%E4%B8%80%E4%B8%AA%E6%95%B0%E6%8D%AE%E5%BA%93%E8%BF%9E%E6%8E%A5%E6%B1%A0/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.gif">
      <meta itemprop="name" content="一年春又来">
      <meta itemprop="description" content="">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="一年春又来">
    </span>
      <header class="post-header">
        <h2 class="post-title" itemprop="name headline">
          <a href="/2021/08/01/docs/pool/%E5%AF%B9%E8%B1%A1%E6%B1%A0%E5%AE%9E%E6%88%98-%E7%BC%96%E5%86%99%E4%B8%80%E4%B8%AA%E6%95%B0%E6%8D%AE%E5%BA%93%E8%BF%9E%E6%8E%A5%E6%B1%A0/" class="post-title-link" itemprop="url">对象池实战-编写一个数据库连接池</a>
        </h2>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">发表于</span>

      <time title="创建时间：2021-08-01 16:49:55" itemprop="dateCreated datePublished" datetime="2021-08-01T16:49:55+08:00">2021-08-01</time>
    </span>
      <span class="post-meta-item">
        <span class="post-meta-item-icon">
          <i class="far fa-calendar-check"></i>
        </span>
        <span class="post-meta-item-text">更新于</span>
        <time title="修改时间：2022-05-10 09:48:48" itemprop="dateModified" datetime="2022-05-10T09:48:48+08:00">2022-05-10</time>
      </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-folder"></i>
      </span>
      <span class="post-meta-item-text">分类于</span>
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/categories/Java/" itemprop="url" rel="index"><span itemprop="name">Java</span></a>
        </span>
    </span>

  
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">
          <h1 id="前言"><a href="#前言" class="headerlink" title="前言"></a>前言</h1><p>通过对象池编写一个数据库连接池，深入学习，巩固对象池用法。</p>
<ul>
<li><p>能代替其它数据库连接池产品</p>
<ul>
<li>Hikari、DBCP、Tomcat、Druid…</li>
</ul>
</li>
<li><p>带监控，随时了解了解连接池的情况</p>
</li>
</ul>
<h1 id="ConnectionPooledObjectFactory"><a href="#ConnectionPooledObjectFactory" class="headerlink" title="ConnectionPooledObjectFactory"></a>ConnectionPooledObjectFactory</h1><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">ConnectionPooledObjectFactory</span></span></span><br><span class="line"><span class="class">  <span class="keyword">implements</span> <span class="title">PooledObjectFactory</span>&lt;<span class="title">MyConnection</span>&gt; </span>&#123;</span><br><span class="line">  <span class="keyword">private</span> ObjectPool&lt;MyConnection&gt; objectPool;</span><br><span class="line"></span><br><span class="line">  <span class="function"><span class="keyword">public</span> ObjectPool&lt;MyConnection&gt; <span class="title">getObjectPool</span><span class="params">()</span> </span>&#123;</span><br><span class="line">    <span class="keyword">return</span> objectPool;</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">setObjectPool</span><span class="params">(ObjectPool&lt;MyConnection&gt; objectPool)</span> </span>&#123;</span><br><span class="line">    <span class="keyword">this</span>.objectPool = objectPool;</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  <span class="meta">@Override</span></span><br><span class="line">  <span class="function"><span class="keyword">public</span> PooledObject&lt;MyConnection&gt; <span class="title">makeObject</span><span class="params">()</span> <span class="keyword">throws</span> Exception </span>&#123;</span><br><span class="line">    Class.forName(<span class="string">&quot;com.mysql.jdbc.Driver&quot;</span>);</span><br><span class="line">    Connection connection = DriverManager.getConnection(</span><br><span class="line">      <span class="string">&quot;jdbc:mysql://localhost:3306/xxxx?useUnicode=true&amp;characterEncoding=UTF-8&amp;autoReconnect=true&quot;</span>,</span><br><span class="line">      <span class="string">&quot;root&quot;</span>,</span><br><span class="line">      <span class="string">&quot;root123&quot;</span></span><br><span class="line">    );</span><br><span class="line">    MyConnection myConnection = <span class="keyword">new</span> MyConnection();</span><br><span class="line">    myConnection.setConnection(connection);</span><br><span class="line">    myConnection.setObjectPool(objectPool);</span><br><span class="line">    <span class="keyword">return</span> <span class="keyword">new</span> DefaultPooledObject&lt;&gt;(myConnection);</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  <span class="meta">@Override</span></span><br><span class="line">  <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">destroyObject</span><span class="params">(PooledObject&lt;MyConnection&gt; p)</span> <span class="keyword">throws</span> Exception </span>&#123;</span><br><span class="line">    p.getObject().close();</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  <span class="meta">@Override</span></span><br><span class="line">  <span class="function"><span class="keyword">public</span> <span class="keyword">boolean</span> <span class="title">validateObject</span><span class="params">(PooledObject&lt;MyConnection&gt; p)</span> </span>&#123;</span><br><span class="line">    Connection connection = p.getObject();</span><br><span class="line">    <span class="keyword">try</span> &#123;</span><br><span class="line">      PreparedStatement statement = connection.prepareStatement(<span class="string">&quot;SELECT 1&quot;</span>);</span><br><span class="line">      ResultSet resultSet = statement.executeQuery();</span><br><span class="line">      <span class="keyword">int</span> i = resultSet.getInt(<span class="number">1</span>);</span><br><span class="line">      <span class="keyword">return</span> i == <span class="number">1</span>;</span><br><span class="line">    &#125; <span class="keyword">catch</span> (SQLException e) &#123;</span><br><span class="line">      <span class="keyword">return</span> <span class="keyword">false</span>;</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  <span class="meta">@Override</span></span><br><span class="line">  <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">activateObject</span><span class="params">(PooledObject&lt;MyConnection&gt; p)</span> <span class="keyword">throws</span> Exception </span>&#123;</span><br><span class="line">    <span class="comment">// 可以把connection额外的配置放到这里</span></span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  <span class="meta">@Override</span></span><br><span class="line">  <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">passivateObject</span><span class="params">(PooledObject&lt;MyConnection&gt; p)</span> <span class="keyword">throws</span> Exception </span>&#123;</span><br><span class="line">    <span class="comment">// 钝化</span></span><br><span class="line">    MyConnection myConnection = p.getObject();</span><br><span class="line">    Statement statement = myConnection.getStatement();</span><br><span class="line">    <span class="keyword">if</span> (statement != <span class="keyword">null</span>) &#123;</span><br><span class="line">      statement.close();</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h1 id="MyDataSource"><a href="#MyDataSource" class="headerlink" title="MyDataSource"></a>MyDataSource</h1><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">MyDataSource</span> <span class="keyword">implements</span> <span class="title">DataSource</span> </span>&#123;</span><br><span class="line">  <span class="keyword">private</span> GenericObjectPool&lt;MyConnection&gt; pool;</span><br><span class="line"></span><br><span class="line">  <span class="function"><span class="keyword">public</span> GenericObjectPool&lt;MyConnection&gt; <span class="title">getPool</span><span class="params">()</span> </span>&#123;</span><br><span class="line">    <span class="keyword">return</span> pool;</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">setPool</span><span class="params">(GenericObjectPool&lt;MyConnection&gt; pool)</span> </span>&#123;</span><br><span class="line">    <span class="keyword">this</span>.pool = pool;</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  <span class="function"><span class="keyword">public</span> <span class="title">MyDataSource</span><span class="params">()</span> </span>&#123;</span><br><span class="line">    ConnectionPooledObjectFactory factory = <span class="keyword">new</span> ConnectionPooledObjectFactory();</span><br><span class="line">    <span class="keyword">this</span>.pool = <span class="keyword">new</span> GenericObjectPool&lt;&gt;(factory);</span><br><span class="line">    factory.setObjectPool(pool);</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  <span class="meta">@Override</span></span><br><span class="line">  <span class="function"><span class="keyword">public</span> Connection <span class="title">getConnection</span><span class="params">()</span> <span class="keyword">throws</span> SQLException </span>&#123;</span><br><span class="line">    <span class="keyword">try</span> &#123;</span><br><span class="line">      <span class="keyword">return</span> <span class="keyword">this</span>.pool.borrowObject();</span><br><span class="line">    &#125; <span class="keyword">catch</span> (Exception e) &#123;</span><br><span class="line">      <span class="keyword">throw</span> <span class="keyword">new</span> SQLException(<span class="string">&quot;获取连接失败！&quot;</span>, e);</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  <span class="meta">@Override</span></span><br><span class="line">  <span class="function"><span class="keyword">public</span> Connection <span class="title">getConnection</span><span class="params">(String username, String password)</span> <span class="keyword">throws</span> SQLException </span>&#123;</span><br><span class="line">    <span class="keyword">return</span> <span class="keyword">this</span>.getConnection();</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  <span class="meta">@Override</span></span><br><span class="line">  <span class="keyword">public</span> &lt;T&gt; <span class="function">T <span class="title">unwrap</span><span class="params">(Class&lt;T&gt; iface)</span> <span class="keyword">throws</span> SQLException </span>&#123;</span><br><span class="line">    <span class="keyword">throw</span> <span class="keyword">new</span> UnsupportedOperationException(<span class="string">&quot;不支持的操作！&quot;</span>);</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  <span class="meta">@Override</span></span><br><span class="line">  <span class="function"><span class="keyword">public</span> <span class="keyword">boolean</span> <span class="title">isWrapperFor</span><span class="params">(Class&lt;?&gt; iface)</span> <span class="keyword">throws</span> SQLException </span>&#123;</span><br><span class="line">    <span class="keyword">throw</span> <span class="keyword">new</span> UnsupportedOperationException(<span class="string">&quot;不支持的操作！&quot;</span>);</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  <span class="meta">@Override</span></span><br><span class="line">  <span class="function"><span class="keyword">public</span> PrintWriter <span class="title">getLogWriter</span><span class="params">()</span> <span class="keyword">throws</span> SQLException </span>&#123;</span><br><span class="line">    <span class="keyword">throw</span> <span class="keyword">new</span> UnsupportedOperationException(<span class="string">&quot;不支持的操作！&quot;</span>);</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  <span class="meta">@Override</span></span><br><span class="line">  <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">setLogWriter</span><span class="params">(PrintWriter out)</span> <span class="keyword">throws</span> SQLException </span>&#123;</span><br><span class="line">    <span class="keyword">throw</span> <span class="keyword">new</span> UnsupportedOperationException(<span class="string">&quot;不支持的操作！&quot;</span>);</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  <span class="meta">@Override</span></span><br><span class="line">  <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">setLoginTimeout</span><span class="params">(<span class="keyword">int</span> seconds)</span> <span class="keyword">throws</span> SQLException </span>&#123;</span><br><span class="line">    <span class="keyword">throw</span> <span class="keyword">new</span> UnsupportedOperationException(<span class="string">&quot;不支持的操作！&quot;</span>);</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  <span class="meta">@Override</span></span><br><span class="line">  <span class="function"><span class="keyword">public</span> <span class="keyword">int</span> <span class="title">getLoginTimeout</span><span class="params">()</span> <span class="keyword">throws</span> SQLException </span>&#123;</span><br><span class="line">    <span class="keyword">throw</span> <span class="keyword">new</span> UnsupportedOperationException(<span class="string">&quot;不支持的操作！&quot;</span>);</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  <span class="meta">@Override</span></span><br><span class="line">  <span class="function"><span class="keyword">public</span> Logger <span class="title">getParentLogger</span><span class="params">()</span> <span class="keyword">throws</span> SQLFeatureNotSupportedException </span>&#123;</span><br><span class="line">    <span class="keyword">throw</span> <span class="keyword">new</span> UnsupportedOperationException(<span class="string">&quot;不支持的操作！&quot;</span>);</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h1 id="MyConnection"><a href="#MyConnection" class="headerlink" title="MyConnection"></a>MyConnection</h1><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 1. 回收statement</span></span><br><span class="line"><span class="comment">// 2. 回收resultSet</span></span><br><span class="line"><span class="comment">// 3. 复用Connection</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">MyConnection</span> <span class="keyword">implements</span> <span class="title">Connection</span> </span>&#123;</span><br><span class="line">  <span class="keyword">private</span> Connection connection;</span><br><span class="line">  <span class="keyword">private</span> Statement statement;</span><br><span class="line">  <span class="keyword">private</span> ObjectPool&lt;MyConnection&gt; objectPool;</span><br><span class="line">  </span><br><span class="line">  <span class="comment">// 篇幅有限，省略其它方法</span></span><br><span class="line">  </span><br><span class="line">  <span class="meta">@Override</span></span><br><span class="line">  <span class="function"><span class="keyword">public</span> Statement <span class="title">createStatement</span><span class="params">()</span> <span class="keyword">throws</span> SQLException </span>&#123;</span><br><span class="line">    <span class="keyword">return</span> <span class="keyword">this</span>.connection.createStatement();</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  <span class="meta">@Override</span></span><br><span class="line">  <span class="function"><span class="keyword">public</span> PreparedStatement <span class="title">prepareStatement</span><span class="params">(String sql)</span> <span class="keyword">throws</span> SQLException </span>&#123;</span><br><span class="line">    PreparedStatement prepareStatement = <span class="keyword">this</span>.connection.prepareStatement(sql);</span><br><span class="line">    <span class="keyword">this</span>.statement = prepareStatement;</span><br><span class="line">    <span class="keyword">return</span> prepareStatement;</span><br><span class="line">  &#125;</span><br><span class="line">  </span><br><span class="line">  <span class="meta">@Override</span></span><br><span class="line">  <span class="function"><span class="keyword">public</span> PreparedStatement <span class="title">prepareStatement</span><span class="params">(String sql, String[] columnNames)</span> <span class="keyword">throws</span> SQLException </span>&#123;</span><br><span class="line">    PreparedStatement statement = <span class="keyword">this</span>.connection.prepareStatement(sql, columnNames);</span><br><span class="line">    <span class="keyword">this</span>.statement = statement;</span><br><span class="line">    <span class="keyword">return</span> statement;</span><br><span class="line">  &#125;</span><br><span class="line">  </span><br><span class="line">  <span class="meta">@Override</span></span><br><span class="line">  <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">close</span><span class="params">()</span> <span class="keyword">throws</span> SQLException </span>&#123;</span><br><span class="line">    <span class="comment">// 如果底层的Connection已经关闭</span></span><br><span class="line">    <span class="keyword">if</span> (<span class="keyword">this</span>.isClosed()) &#123;</span><br><span class="line">      <span class="keyword">try</span> &#123;</span><br><span class="line">        objectPool.invalidateObject(<span class="keyword">this</span>);</span><br><span class="line">      &#125; <span class="keyword">catch</span> (Exception e) &#123;</span><br><span class="line">        <span class="keyword">throw</span> <span class="keyword">new</span> SQLException(e);</span><br><span class="line">      &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">// 底层Connection没有关闭，可以继续复用</span></span><br><span class="line">    <span class="keyword">else</span> &#123;</span><br><span class="line">      <span class="keyword">try</span> &#123;</span><br><span class="line">        objectPool.returnObject(<span class="keyword">this</span>);</span><br><span class="line">      &#125; <span class="keyword">catch</span> (Exception e) &#123;</span><br><span class="line">        <span class="keyword">this</span>.connection.close();</span><br><span class="line">        <span class="keyword">throw</span> <span class="keyword">new</span> SQLException(e);</span><br><span class="line">      &#125;</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line">  </span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h1 id="如何使用？"><a href="#如何使用？" class="headerlink" title="如何使用？"></a>如何使用？</h1><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Bean</span></span><br><span class="line"><span class="meta">@Primary</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> DataSource <span class="title">dataSource</span><span class="params">()</span></span>&#123;</span><br><span class="line">    <span class="keyword">return</span> <span class="keyword">new</span> DMDataSource();</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h1 id="如何使用-spring-boot-starter-actuator-添加监控？"><a href="#如何使用-spring-boot-starter-actuator-添加监控？" class="headerlink" title="如何使用 spring-boot-starter-actuator 添加监控？"></a>如何使用 <code>spring-boot-starter-actuator</code> 添加监控？</h1><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 可以用/actuator/datasource</span></span><br><span class="line"><span class="meta">@Endpoint(id = &quot;datasource&quot;)</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">DataSourceEnpoint</span> </span>&#123;</span><br><span class="line">  <span class="keyword">private</span> MyDataSource dataSource;</span><br><span class="line"></span><br><span class="line">  <span class="function"><span class="keyword">public</span> <span class="title">DataSourceEnpoint</span><span class="params">(MyDataSource dataSource)</span> </span>&#123;</span><br><span class="line">    <span class="keyword">this</span>.dataSource = dataSource;</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  <span class="meta">@ReadOperation</span></span><br><span class="line">  <span class="function"><span class="keyword">public</span> Map&lt;String, Object&gt; <span class="title">pool</span><span class="params">()</span> </span>&#123;</span><br><span class="line">    GenericObjectPool&lt;MyConnection&gt; pool = dataSource.getPool();</span><br><span class="line">    HashMap&lt;String, Object&gt; map = Maps.newHashMap();</span><br><span class="line">    map.put(<span class="string">&quot;numActive&quot;</span>, pool.getNumActive());</span><br><span class="line">    map.put(<span class="string">&quot;numIdle&quot;</span>, pool.getNumIdle());</span><br><span class="line">    map.put(<span class="string">&quot;createdCount&quot;</span>, pool.getCreatedCount());</span><br><span class="line">    <span class="keyword">return</span> map;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>


      
    </div>

    
    
    

    <footer class="post-footer">
        <div class="post-eof"></div>
      
    </footer>
  </article>
</div>




    


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="">
    <link itemprop="mainEntityOfPage" href="http://littlefxc.github.io/2021/08/01/docs/pool/%E5%AF%B9%E8%B1%A1%E6%B1%A0-commons-pool2/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.gif">
      <meta itemprop="name" content="一年春又来">
      <meta itemprop="description" content="">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="一年春又来">
    </span>
      <header class="post-header">
        <h2 class="post-title" itemprop="name headline">
          <a href="/2021/08/01/docs/pool/%E5%AF%B9%E8%B1%A1%E6%B1%A0-commons-pool2/" class="post-title-link" itemprop="url">对象池 commons-pool2</a>
        </h2>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">发表于</span>

      <time title="创建时间：2021-08-01 13:48:21" itemprop="dateCreated datePublished" datetime="2021-08-01T13:48:21+08:00">2021-08-01</time>
    </span>
      <span class="post-meta-item">
        <span class="post-meta-item-icon">
          <i class="far fa-calendar-check"></i>
        </span>
        <span class="post-meta-item-text">更新于</span>
        <time title="修改时间：2022-05-10 09:48:38" itemprop="dateModified" datetime="2022-05-10T09:48:38+08:00">2022-05-10</time>
      </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-folder"></i>
      </span>
      <span class="post-meta-item-text">分类于</span>
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/categories/Java/" itemprop="url" rel="index"><span itemprop="name">Java</span></a>
        </span>
    </span>

  
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">
          <h1 id="1-前言"><a href="#1-前言" class="headerlink" title="1 前言"></a>1 前言</h1><p>池化技术是性能调优的重要措施，池化的思想是把对象放到池子里面，当要使用的时候从池子里面拿对象，用完之后在放回池子里面。这样可以降低资源分配和释放资源的开销，从而提升性能。</p>
          <!--noindex-->
            <div class="post-button">
              <a class="btn" href="/2021/08/01/docs/pool/%E5%AF%B9%E8%B1%A1%E6%B1%A0-commons-pool2/#more" rel="contents">
                阅读全文 &raquo;
              </a>
            </div>
          <!--/noindex-->
        
      
    </div>

    
    
    

    <footer class="post-footer">
        <div class="post-eof"></div>
      
    </footer>
  </article>
</div>




    


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="">
    <link itemprop="mainEntityOfPage" href="http://littlefxc.github.io/2021/07/28/Java-Minor-GC%E3%80%81Major-GC-%E5%92%8C-Full-GC%E4%B9%8B%E9%97%B4%E7%9A%84%E5%8C%BA%E5%88%AB%E4%BB%A5%E5%8F%8A%E8%A7%A6%E5%8F%91%E6%9D%A1%E4%BB%B6/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.gif">
      <meta itemprop="name" content="一年春又来">
      <meta itemprop="description" content="">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="一年春又来">
    </span>
      <header class="post-header">
        <h2 class="post-title" itemprop="name headline">
          <a href="/2021/07/28/Java-Minor-GC%E3%80%81Major-GC-%E5%92%8C-Full-GC%E4%B9%8B%E9%97%B4%E7%9A%84%E5%8C%BA%E5%88%AB%E4%BB%A5%E5%8F%8A%E8%A7%A6%E5%8F%91%E6%9D%A1%E4%BB%B6/" class="post-title-link" itemprop="url">Java Minor GC、Major GC 和 Full GC之间的区别以及触发条件</a>
        </h2>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">发表于</span>

      <time title="创建时间：2021-07-28 14:43:13" itemprop="dateCreated datePublished" datetime="2021-07-28T14:43:13+08:00">2021-07-28</time>
    </span>
      <span class="post-meta-item">
        <span class="post-meta-item-icon">
          <i class="far fa-calendar-check"></i>
        </span>
        <span class="post-meta-item-text">更新于</span>
        <time title="修改时间：2022-05-10 09:44:35" itemprop="dateModified" datetime="2022-05-10T09:44:35+08:00">2022-05-10</time>
      </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-folder"></i>
      </span>
      <span class="post-meta-item-text">分类于</span>
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/categories/Java/" itemprop="url" rel="index"><span itemprop="name">Java</span></a>
        </span>
    </span>

  
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">
          <blockquote>
<p>转载自<a target="_blank" rel="noopener" href="https://blog.csdn.net/luzhensmart/article/details/103782340">https://blog.csdn.net/luzhensmart/article/details/103782340</a></p>
</blockquote>
<h3 id="1-Java-Minor-GC、Major-GC和Full-GC之间的区别"><a href="#1-Java-Minor-GC、Major-GC和Full-GC之间的区别" class="headerlink" title="1: Java Minor GC、Major GC和Full GC之间的区别"></a>1: Java Minor GC、Major GC和Full GC之间的区别</h3><ul>
<li><p>Minor GC</p>
<p>Minor GC指新生代GC，即发生在新生代（包括Eden区和Survivor区）的垃圾回收操作，当新生代无法为新生对象分配内存空间的时候，会触发Minor GC。因为新生代中大多数对象的生命周期都很短，所以发生Minor GC的频率很高，虽然它会触发stop-the-world，但是它的回收速度很快。</p>
          <!--noindex-->
            <div class="post-button">
              <a class="btn" href="/2021/07/28/Java-Minor-GC%E3%80%81Major-GC-%E5%92%8C-Full-GC%E4%B9%8B%E9%97%B4%E7%9A%84%E5%8C%BA%E5%88%AB%E4%BB%A5%E5%8F%8A%E8%A7%A6%E5%8F%91%E6%9D%A1%E4%BB%B6/#more" rel="contents">
                阅读全文 &raquo;
              </a>
            </div>
          <!--/noindex-->
        
      
    </div>

    
    
    

    <footer class="post-footer">
        <div class="post-eof"></div>
      
    </footer>
  </article>
</div>




    


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="">
    <link itemprop="mainEntityOfPage" href="http://littlefxc.github.io/2021/07/28/docs/JVM/Java-Minor-GC%E3%80%81Major-GC-%E5%92%8C-Full-GC%E4%B9%8B%E9%97%B4%E7%9A%84%E5%8C%BA%E5%88%AB%E4%BB%A5%E5%8F%8A%E8%A7%A6%E5%8F%91%E6%9D%A1%E4%BB%B6/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.gif">
      <meta itemprop="name" content="一年春又来">
      <meta itemprop="description" content="">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="一年春又来">
    </span>
      <header class="post-header">
        <h2 class="post-title" itemprop="name headline">
          <a href="/2021/07/28/docs/JVM/Java-Minor-GC%E3%80%81Major-GC-%E5%92%8C-Full-GC%E4%B9%8B%E9%97%B4%E7%9A%84%E5%8C%BA%E5%88%AB%E4%BB%A5%E5%8F%8A%E8%A7%A6%E5%8F%91%E6%9D%A1%E4%BB%B6/" class="post-title-link" itemprop="url">Java Minor GC、Major GC 和 Full GC之间的区别以及触发条件</a>
        </h2>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">发表于</span>

      <time title="创建时间：2021-07-28 06:43:13" itemprop="dateCreated datePublished" datetime="2021-07-28T06:43:13+08:00">2021-07-28</time>
    </span>
      <span class="post-meta-item">
        <span class="post-meta-item-icon">
          <i class="far fa-calendar-check"></i>
        </span>
        <span class="post-meta-item-text">更新于</span>
        <time title="修改时间：2022-05-10 09:23:49" itemprop="dateModified" datetime="2022-05-10T09:23:49+08:00">2022-05-10</time>
      </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-folder"></i>
      </span>
      <span class="post-meta-item-text">分类于</span>
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/categories/Java/" itemprop="url" rel="index"><span itemprop="name">Java</span></a>
        </span>
    </span>

  
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">
          <blockquote>
<p>转载自<a target="_blank" rel="noopener" href="https://blog.csdn.net/luzhensmart/article/details/103782340">https://blog.csdn.net/luzhensmart/article/details/103782340</a></p>
</blockquote>
<h3 id="1-Java-Minor-GC、Major-GC和Full-GC之间的区别"><a href="#1-Java-Minor-GC、Major-GC和Full-GC之间的区别" class="headerlink" title="1: Java Minor GC、Major GC和Full GC之间的区别"></a>1: Java Minor GC、Major GC和Full GC之间的区别</h3><ul>
<li><p>Minor GC</p>
<p>Minor GC指新生代GC，即发生在新生代（包括Eden区和Survivor区）的垃圾回收操作，当新生代无法为新生对象分配内存空间的时候，会触发Minor GC。因为新生代中大多数对象的生命周期都很短，所以发生Minor GC的频率很高，虽然它会触发stop-the-world，但是它的回收速度很快。</p>
          <!--noindex-->
            <div class="post-button">
              <a class="btn" href="/2021/07/28/docs/JVM/Java-Minor-GC%E3%80%81Major-GC-%E5%92%8C-Full-GC%E4%B9%8B%E9%97%B4%E7%9A%84%E5%8C%BA%E5%88%AB%E4%BB%A5%E5%8F%8A%E8%A7%A6%E5%8F%91%E6%9D%A1%E4%BB%B6/#more" rel="contents">
                阅读全文 &raquo;
              </a>
            </div>
          <!--/noindex-->
        
      
    </div>

    
    
    

    <footer class="post-footer">
        <div class="post-eof"></div>
      
    </footer>
  </article>
</div>




    


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="">
    <link itemprop="mainEntityOfPage" href="http://littlefxc.github.io/2021/07/27/%E9%93%BE%E8%B7%AF%E8%BF%BD%E8%B8%AAzipkin/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.gif">
      <meta itemprop="name" content="一年春又来">
      <meta itemprop="description" content="">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="一年春又来">
    </span>
      <header class="post-header">
        <h2 class="post-title" itemprop="name headline">
          <a href="/2021/07/27/%E9%93%BE%E8%B7%AF%E8%BF%BD%E8%B8%AAzipkin/" class="post-title-link" itemprop="url">链路追踪zipkin</a>
        </h2>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">发表于</span>

      <time title="创建时间：2021-07-27 13:34:25" itemprop="dateCreated datePublished" datetime="2021-07-27T13:34:25+08:00">2021-07-27</time>
    </span>

  
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">
          
      
    </div>

    
    
    

    <footer class="post-footer">
        <div class="post-eof"></div>
      
    </footer>
  </article>
</div>




    


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="">
    <link itemprop="mainEntityOfPage" href="http://littlefxc.github.io/2021/07/24/%E5%88%86%E5%B8%83%E5%BC%8F%E4%BA%8B%E5%8A%A1/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.gif">
      <meta itemprop="name" content="一年春又来">
      <meta itemprop="description" content="">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="一年春又来">
    </span>
      <header class="post-header">
        <h2 class="post-title" itemprop="name headline">
          <a href="/2021/07/24/%E5%88%86%E5%B8%83%E5%BC%8F%E4%BA%8B%E5%8A%A1/" class="post-title-link" itemprop="url">分布式事务</a>
        </h2>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">发表于</span>

      <time title="创建时间：2021-07-24 16:38:01" itemprop="dateCreated datePublished" datetime="2021-07-24T16:38:01+08:00">2021-07-24</time>
    </span>
      <span class="post-meta-item">
        <span class="post-meta-item-icon">
          <i class="far fa-calendar-check"></i>
        </span>
        <span class="post-meta-item-text">更新于</span>
        <time title="修改时间：2022-05-10 09:51:45" itemprop="dateModified" datetime="2022-05-10T09:51:45+08:00">2022-05-10</time>
      </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-folder"></i>
      </span>
      <span class="post-meta-item-text">分类于</span>
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/categories/%E5%88%86%E5%B8%83%E5%BC%8F%E7%B3%BB%E7%BB%9F/" itemprop="url" rel="index"><span itemprop="name">分布式系统</span></a>
        </span>
    </span>

  
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">
          <h1 id="1-概述"><a href="#1-概述" class="headerlink" title="1 概述"></a>1 概述</h1><ul>
<li><p>CAP原理</p>
</li>
<li><p>ACID原理与BASE原理</p>
</li>
<li><p>基于XA协议的两阶段提交</p>
</li>
<li><p>事务补偿机制</p>
</li>
<li><p>基于本地消息表的最终一致方案</p>
</li>
<li><p>基于MQ消息队列的最终一致方案</p>
</li>
</ul>
<h1 id="2-CAP原理"><a href="#2-CAP原理" class="headerlink" title="2 CAP原理"></a>2 CAP原理</h1><h2 id="2-1-简介"><a href="#2-1-简介" class="headerlink" title="2.1 简介"></a>2.1 简介</h2><p>在分布式系统中，我们经常听到CAP原理这个词，它是什么意思呢？其实和C、A、P这3个字母有关，C、A、P分别是这3个词的首字母。下面我们就看一下这3个词分别是什么意思？</p>
<ul>
<li>C - Consistent ，一致性。具体是指，操作成功以后，所有的节点，在同一时间，看到的数据都是完全一致的。所以，一致性，说的就是数据一致性。</li>
<li>A - Availability ，可用性。指服务一致可用，在规定的时间内完成响应。</li>
<li>P - Partition tolerance ，分区容错性。指分布式系统在遇到某节点或网络分区故障的时候，仍然能够对外提供服务。</li>
</ul>
<p><img src="https://gitee.com/littlefxc/oss/raw/master/images/5e0f0c0c0934d12d06200487.png"></p>
<p>CAP原理指出，这3个指标不能同时满足，最多只能满足其中的两个。</p>
<h2 id="2-2-详解"><a href="#2-2-详解" class="headerlink" title="2.2 详解"></a>2.2 详解</h2><p>我们之所以使用分布式系统，就是为了在某个节点不可用的情况下，整个服务对外还是可用的，这正是满足P（分区容错性）。如果我们的服务不满足P（分区容错性），那么我们的系统也就不是分布式系统了，所以，在分布式系统中，P（分布容错性）总是成立的。那么，A（可用性）和C（一致性）能不能同时满足呢？我们看一下下面的图例。</p>
<p><img src="https://gitee.com/littlefxc/oss/raw/master/images/5e0f0c24092a8a2205300403.png"></p>
<p>A和B是两个数据节点，A向B同步数据，并且作为一个整体对外提供服务。由于我们的系统保证了P（分区容错性），那么A和B的同步，我们允许出现故障。接下来我们再保证A（可用性），也就是说A和B同步出现问题时，客户端还能够访问我们的系统，那么客户端既可能访问A也可能访问B，这时，A和B的数据是不一致的，所以C（一致性）不能满足。</p>
<p>如果我们满足C（一致性），也就是说客户端无论访问A还是访问B，得到的结果都是一样的，那么现在A和B的数据不一致，需要等到A和B的数据一致以后，也就是同步恢复以后，才可对外提供服务。这样我们虽然满足了C（一致性），却不能满足A（可用性）。</p>
<p>所以，我们的系统在满足P（分区容错性）的同时，只能在A（可用性）和C（一致性）当中选择一个不能CAP同时满足。我们的分布式系统只能是AP或者CP。</p>
<h1 id="3-ACID与BASE"><a href="#3-ACID与BASE" class="headerlink" title="3 ACID与BASE"></a>3 ACID与BASE</h1><p>在关系型数据库中，最大的特点就是事务处理，也就是ACID。ACID是事务处理的4个特性。</p>
<ul>
<li><p>A - Atomicity（原子性），事务中的操作要么都做，要么都不做。</p>
</li>
<li><p>C - Consistency（一致性），系统必须始终处在强一致状态下。</p>
</li>
<li><p>I - Isolation（隔离性），一个事务的执行不能被其他事务所干扰。</p>
</li>
<li><p>D - Durability（持久性），一个已提交的事务对数据库中数据的改变是永久性的。</p>
</li>
</ul>
<p>ACID强调的是强一致性，要么全做，要么全不做，所有的用户看到的都是一致的数据。传统的数据库都有ACID特性，它们在CAP原理中，保证的是CA。但是在分布式系统大行其道的今天，满足CA特性的系统很难生存下去。ACID也逐渐的向BASE转换。那么什么是BASE呢？</p>
<p> BASE是Basically Available（基本可用）, Soft-state（软状态）, Eventually consistent（最终一致）的缩写。</p>
<ul>
<li><p>Basically Available，基本可用是指分布式系统在出现故障的时候，允许损失部分可用性，即保证核心可用。</p>
<p>电商大促时，为了应对访问量激增，部分用户可能会被引导到降级页面，服务层也可能只提供降级服务。这就是损失部分可用性的体现。</p>
</li>
<li><p>软状态（ Soft State）</p>
<p>软状态是指允许系统存在中间状态，而该中间状态不会影响系统整体可用性。分布式存储中一般一份数据至少会有两到三个副本，允许不同节点间副本同步的延时就是软状态的体现。mysql replication的异步复制也是一种体现。</p>
</li>
<li><p>最终一致性（ Eventual Consistency）</p>
<p>最终一致性是指系统中的所有数据副本经过一定时间后，最终能够达到一致的状态。弱一致性和强一致性相反，最终一致性是弱一致性的一种特殊情况。</p>
</li>
</ul>
<p>BASE模型是传统ACID模型的反面，不同与ACID，BASE强调牺牲高一致性，从而获得可用性，数据允许在一段时间内的不一致，只要保证最终一致就可以了。</p>
<p>在分布式事务的解决方案中，它们都是依赖了ACID或者BASE的模型而实现的。像基于XA协议的两阶段提交和实物补偿机制就是基于ACID实现的。而基于本地消息表和基于MQ的最终一致方案都是通过BASE原理实现的。这几种分布式事务的解决方案，我们会在视频的课程中给大家讲解。</p>
<h1 id="4-由分库分表引发的事物问题和解决方案"><a href="#4-由分库分表引发的事物问题和解决方案" class="headerlink" title="4 由分库分表引发的事物问题和解决方案"></a>4 由分库分表引发的事物问题和解决方案</h1><h2 id="4-1-由分库分表引发的事物问题"><a href="#4-1-由分库分表引发的事物问题" class="headerlink" title="4.1 由分库分表引发的事物问题"></a>4.1 由分库分表引发的事物问题</h2><p>传统的应用都是单一数据库事物，所有的业务表都在同一数据库内，这时候数据库的事物可以得到很好的支持，如下图所示：</p>
<p><img src="https://gitee.com/littlefxc/oss/raw/master/images/image-20210724171356938.png" alt="image-20210724171356938"></p>
<p>然后在同一个业务内，再把数据库进行水平切分多个数据库，多个数据库之间是没有办法无法统一事务管理的，就会造成数据不一致的情况。如果按照垂直切分成多个数据库，如下图所示：</p>
<p><img src="https://gitee.com/littlefxc/oss/raw/master/images/image-20210724172152429.png" alt="image-20210724172152429"></p>
<p>比如：</p>
<ul>
<li><p>一个下单操作，用户使用积分购买商品</p>
</li>
<li><p>用户库扣减积分，订单库生成订单，商品库扣减库存</p>
</li>
<li><p>由于它们不在同一数据库，不能改保证事务统一</p>
</li>
<li><p>任何一个环节出错，其他两个数据的事务将不能回滚</p>
</li>
</ul>
<h2 id="4-2-解决方案"><a href="#4-2-解决方案" class="headerlink" title="4.2 解决方案"></a>4.2 解决方案</h2><p>那么如何解决呢？</p>
<ul>
<li>基于XA协议的两阶段提交</li>
<li>事务补偿机制</li>
<li>基于本地消息表+定时任务的最终一致性方案</li>
<li>基于MQ的最终一致方案</li>
</ul>
<h3 id="4-2-1-基于XA协议的两阶段提交"><a href="#4-2-1-基于XA协议的两阶段提交" class="headerlink" title="4.2.1 基于XA协议的两阶段提交"></a>4.2.1 基于XA协议的两阶段提交</h3><p><strong>XA 协议原理简介</strong></p>
<ul>
<li><p>XA 是由 X/Open 组织提出的分布式事务的规范</p>
</li>
<li><p>有一个事务管理器（TM）和多个资源管理器（RM）组成</p>
<p>TM：数据源，RM：数据库</p>
</li>
<li><p>提交阶段分为两个阶段：prepare 和 commit</p>
<p>也就是说在提交之前，让所有的数据库先进行准备，准备好了之后通知事务管理器，然后由事务管理器统一的提交事务，如下图所示：</p>
<p><img src="https://gitee.com/littlefxc/oss/raw/master/images/image-20210724175921780.png" alt="image-20210724175921780"></p>
<p><img src="https://gitee.com/littlefxc/oss/raw/master/images/image-20210724180456245.png" alt="image-20210724180456245"></p>
</li>
<li><p>XA协议保证了数据的强一致性</p>
</li>
<li><p>commit 阶段出现问题，事务不一致，需人工处理</p>
</li>
<li><p>但是两阶段提交效率低下，性能与本地事务相差 10 倍</p>
</li>
<li><p>MySql5.7 及以上均支持XA协议</p>
</li>
<li><p>Mysql Connector/J 5.0以上支持XA协议</p>
</li>
<li><p>Java系统中，数据源可以采用 Atomikos（充当事务管理器）</p>
</li>
</ul>
<h4 id="4-2-1-1-使用Mycat-或者Sharding-Jdbc"><a href="#4-2-1-1-使用Mycat-或者Sharding-Jdbc" class="headerlink" title="4.2.1.1 使用Mycat 或者Sharding-Jdbc"></a>4.2.1.1 使用Mycat 或者Sharding-Jdbc</h4><p><strong>配置Mycat</strong></p>
<p>通过修改<code>conf/server.xml</code>配置分布式事务，如下图所示：</p>
<p><img src="https://gitee.com/littlefxc/oss/raw/master/images/5ec4e2810969290517080353.jpg"></p>
<p>至于Sharding-Jdbc则默认为我们开启了分布式事务</p>
<h3 id="4-2-2-事务补偿机制"><a href="#4-2-2-事务补偿机制" class="headerlink" title="4.2.2 事务补偿机制"></a>4.2.2 事务补偿机制</h3><p><strong>什么事务补偿机制？</strong></p>
<p>其实就是针对每个操作，都要注册一个与其对应的补偿（撤销）操作，在执行失败时，调用补偿操作，撤销之前的操作。</p>
<p>例如一个A给B转账的例子：</p>
<p><img src="https://gitee.com/littlefxc/oss/raw/master/images/image-20210725155803128.png" alt="image-20210725155803128"></p>
<ul>
<li>A 和 B 在不两家不同的银行</li>
<li>A 账户减 200元，B 账户加 200 元</li>
<li>两个操作要保证原子性，要么都成功，要么都失败</li>
<li>由于A和B在两家不同的银行，所以存在分布式事务问题</li>
<li>转账接口需要提供补偿机制</li>
<li>如果A在扣减的过程中出现问题，直接抛出异常，事务回滚</li>
<li>B在增加余额的过程中，出现问题，要调用A的补偿接口</li>
<li>A之前的扣减操作，得到了补偿，进行了撤销</li>
<li>保证了A和B的帐是没有问题的</li>
</ul>
<p><strong>如果A补偿操作也失败了呢？</strong></p>
<p>我们可以为A补偿操作添加有限次数的重试机制，超出重试次数后，人工介入。</p>
<p><strong>事务补偿机制看起来有点繁琐，不推荐使用。</strong></p>
<p><strong>事务补偿机制的优缺点：</strong></p>
<ul>
<li><p>优点：逻辑清晰、流程简单</p>
</li>
<li><p>缺点：数据一致性比XA还要差，可能出错的点比较多</p>
</li>
<li><p>TCC属于应用层的一种补偿方式，程序员需要写大量的代码</p>
</li>
</ul>
<h3 id="4-2-3-基于本地消息表的的最终一致性方案"><a href="#4-2-3-基于本地消息表的的最终一致性方案" class="headerlink" title="4.2.3 基于本地消息表的的最终一致性方案"></a>4.2.3 基于本地消息表的的最终一致性方案</h3><p><img src="https://gitee.com/littlefxc/oss/raw/master/images/image-20210726213335173.png" alt="image-20210726213335173"></p>
<ul>
<li>采用BASE原理，保证事务最终一致</li>
<li>在一致性方面，允许一段时间内的不一致，但最终会一致</li>
<li>在实际的系统中，要根据具体情况，判断是否采用</li>
<li>基于本地消息表的方案中，将<font color=red><strong>本事务外</strong></font>操作，记录在消息表中</li>
<li>其他的事务要提供操作接口（例如支付宝、微信支付的接口）</li>
<li>定时任务轮训本地消息表，将未执行的的消息发送给操作接口</li>
<li>操作接口处理成功，返回成功标识，处理失败返回失败标识</li>
<li>定时任务接到标识，更新消息的状态</li>
<li>定时任务按照一定的周期反复执行</li>
<li>对于屡次失败的消息，可以设置最大失败次数</li>
<li>超过最大失败次数的消息，不再进行接口调用</li>
<li>等待人工处理</li>
</ul>
<p>那么这个方案有什么优缺点呢？</p>
<ul>
<li>优点：避免了了分布式事务，实现了最终一致性</li>
<li>缺点：要注意重试时的幂等性操作</li>
</ul>
<h3 id="4-2-4-基于MQ的最终一致性方案"><a href="#4-2-4-基于MQ的最终一致性方案" class="headerlink" title="4.2.4 基于MQ的最终一致性方案"></a>4.2.4 基于MQ的最终一致性方案</h3><ul>
<li>原理、流程与本地消息表类似</li>
<li>不同点1：本地消息表改为 MQ</li>
<li>不同点2：定时任务改为MQ的消费者</li>
</ul>
<p>这样就不在依赖于定时任务了，架构图如下所示：</p>
<p><img src="https://gitee.com/littlefxc/oss/raw/master/images/image-20210728232239034.png" alt="image-20210728232239034"></p>
<ol>
<li><p>首先，业务系统将数据保存到数据库中</p>
</li>
<li><p>然后，业务系统把另外一条消息存放到消息队列中</p>
</li>
<li><p>消费者从消息队列中去取消息</p>
</li>
<li><p>消费者执行分布式事务的第二段的服务，把数据落入到数据库中去</p>
</li>
</ol>
<p><strong>优点</strong></p>
<ul>
<li>不依赖于定时任务，基于MQ更高效、更可靠</li>
<li>适合公司内部的系统</li>
<li>不同公司之间无法基于MQ，本地消息表更合适</li>
</ul>

      
    </div>

    
    
    

    <footer class="post-footer">
        <div class="post-eof"></div>
      
    </footer>
  </article>
</div>




    


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="">
    <link itemprop="mainEntityOfPage" href="http://littlefxc.github.io/2021/07/18/%E5%88%86%E5%B8%83%E5%BC%8F%E5%85%A8%E5%B1%80ID/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.gif">
      <meta itemprop="name" content="一年春又来">
      <meta itemprop="description" content="">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="一年春又来">
    </span>
      <header class="post-header">
        <h2 class="post-title" itemprop="name headline">
          <a href="/2021/07/18/%E5%88%86%E5%B8%83%E5%BC%8F%E5%85%A8%E5%B1%80ID/" class="post-title-link" itemprop="url">分布式全局ID</a>
        </h2>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">发表于</span>

      <time title="创建时间：2021-07-18 17:22:17" itemprop="dateCreated datePublished" datetime="2021-07-18T17:22:17+08:00">2021-07-18</time>
    </span>
      <span class="post-meta-item">
        <span class="post-meta-item-icon">
          <i class="far fa-calendar-check"></i>
        </span>
        <span class="post-meta-item-text">更新于</span>
        <time title="修改时间：2022-05-10 09:51:06" itemprop="dateModified" datetime="2022-05-10T09:51:06+08:00">2022-05-10</time>
      </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-folder"></i>
      </span>
      <span class="post-meta-item-text">分类于</span>
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/categories/%E5%88%86%E5%B8%83%E5%BC%8F%E7%B3%BB%E7%BB%9F/" itemprop="url" rel="index"><span itemprop="name">分布式系统</span></a>
        </span>
    </span>

  
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">
          <h1 id="1-前言"><a href="#1-前言" class="headerlink" title="1 前言"></a>1 前言</h1><p>在分库分表的过程中，因为拆分的实体表的ID有可能是重复的，正式由于这个问题才会有分布式全局ID这个方案的出现。</p>
          <!--noindex-->
            <div class="post-button">
              <a class="btn" href="/2021/07/18/%E5%88%86%E5%B8%83%E5%BC%8F%E5%85%A8%E5%B1%80ID/#more" rel="contents">
                阅读全文 &raquo;
              </a>
            </div>
          <!--/noindex-->
        
      
    </div>

    
    
    

    <footer class="post-footer">
        <div class="post-eof"></div>
      
    </footer>
  </article>
</div>




    


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="">
    <link itemprop="mainEntityOfPage" href="http://littlefxc.github.io/2021/07/11/%E5%9F%BA%E4%BA%8ESharding-Jdbc%E7%9A%84%E8%AF%BB%E5%86%99%E5%88%86%E7%A6%BB%E5%92%8C%E5%88%86%E5%BA%93%E5%88%86%E8%A1%A8/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.gif">
      <meta itemprop="name" content="一年春又来">
      <meta itemprop="description" content="">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="一年春又来">
    </span>
      <header class="post-header">
        <h2 class="post-title" itemprop="name headline">
          <a href="/2021/07/11/%E5%9F%BA%E4%BA%8ESharding-Jdbc%E7%9A%84%E8%AF%BB%E5%86%99%E5%88%86%E7%A6%BB%E5%92%8C%E5%88%86%E5%BA%93%E5%88%86%E8%A1%A8/" class="post-title-link" itemprop="url">基于Sharding-Jdbc的读写分离和分库分表</a>
        </h2>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">发表于</span>
      

      <time title="创建时间：2021-07-11 11:37:33 / 修改时间：12:37:16" itemprop="dateCreated datePublished" datetime="2021-07-11T11:37:33+08:00">2021-07-11</time>
    </span>

  
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">
          <h1 id="简介"><a href="#简介" class="headerlink" title="简介"></a>简介</h1><ul>
<li>是一个开源的分布式的关系型数据库的中间件</li>
<li>已于2020年4月16日成为 Apache 软件基金会的顶级项目</li>
<li>客户端代理模式</li>
<li>定位为轻量级的Java框架，以 jar 包提供服务</li>
<li>可以理解为增强版的 jdbc 驱动</li>
<li>完全兼容各种 ORM 框架</li>
</ul>
          <!--noindex-->
            <div class="post-button">
              <a class="btn" href="/2021/07/11/%E5%9F%BA%E4%BA%8ESharding-Jdbc%E7%9A%84%E8%AF%BB%E5%86%99%E5%88%86%E7%A6%BB%E5%92%8C%E5%88%86%E5%BA%93%E5%88%86%E8%A1%A8/#more" rel="contents">
                阅读全文 &raquo;
              </a>
            </div>
          <!--/noindex-->
        
      
    </div>

    
    
    

    <footer class="post-footer">
        <div class="post-eof"></div>
      
    </footer>
  </article>
</div>




  <nav class="pagination">
    <a class="extend prev" rel="prev" href="/page/4/"><i class="fa fa-angle-left" aria-label="上一页"></i></a><a class="page-number" href="/">1</a><span class="space">&hellip;</span><a class="page-number" href="/page/4/">4</a><span class="page-number current">5</span><a class="page-number" href="/page/6/">6</a><span class="space">&hellip;</span><a class="page-number" href="/page/24/">24</a><a class="extend next" rel="next" href="/page/6/"><i class="fa fa-angle-right" aria-label="下一页"></i></a>
  </nav>


<script>
  window.addEventListener('tabs:register', () => {
    let { activeClass } = CONFIG.comments;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      const activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      const commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }
</script>
</div>
  </main>

  <footer class="footer">
    <div class="footer-inner">


<div class="copyright">
  &copy; 
  <span itemprop="copyrightYear">2022</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">一年春又来</span>
</div>
  <div class="powered-by">由 <a href="https://hexo.io/" class="theme-link" rel="noopener" target="_blank">Hexo</a> & <a href="https://theme-next.js.org/mist/" class="theme-link" rel="noopener" target="_blank">NexT.Mist</a> 强力驱动
  </div>

    </div>
  </footer>

  
  <script src="https://cdn.jsdelivr.net/npm/animejs@3.2.1/lib/anime.min.js"></script>
<script src="/js/utils.js"></script><script src="/js/motion.js"></script><script src="/js/schemes/muse.js"></script><script src="/js/next-boot.js"></script>

  
<script src="/js/local-search.js"></script>






  





</body>
</html>
